package com.leyou.user.web;

import com.leyou.auth.dto.UserDetails;
import com.leyou.auth.utils.UserContext;
import com.leyou.common.exceptions.LyException;
import com.leyou.user.entity.User;
import com.leyou.user.dto.UserDTO;
import com.leyou.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/info")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 校验手机号或用户名是否存在
     * @param data 用户名或手机号
     * @param type 数据类型：1是用户名；2是手机；其它是参数有误
     * @return true：可以使用; false：不可使用
     */
    @GetMapping("/exists/{data}/{type}")
    public ResponseEntity<Boolean> isDataExists(@PathVariable("data") String data, @PathVariable("type") Integer type){
        return ResponseEntity.ok(userService.isDataExists(data,type));
    }


    /**
     * 发送短信验证码
     * @return 无
     */
    @PostMapping("/code")
    public ResponseEntity<Void> sendVerifyCode(@RequestParam("phone") String phone){
        userService.sendVerifyCode(phone);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 注册用户
     * @param user 用户信息
     * @param code 验证码
     * @return 无
     */
    //@Valid 表示user需要被校验   BindingResult 自定义校验的返回结果
    @PostMapping
    public ResponseEntity<Void> register(@Valid User user, BindingResult result, @RequestParam("code") String code){
        //校验判断结果
        if (result.hasErrors()){
            throw new LyException(400,"请求参数有误！");
        }
        userService.register(user,code);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 根据用户名和密码查询用户
     * @param username 手机号
     * @param password 密码
     * @return 用户信息
     */
    @GetMapping
    public ResponseEntity<UserDTO> queryUserByNameAndPassword(@RequestParam("username") String username,@RequestParam("password") String password){
        UserDTO userDTO = userService.queryUserByNameAndPassword(username,password);
        return ResponseEntity.ok(userDTO);
    }

    /**
     * 获取当前登录的用户信息
     * @return 用户信息
     */
    @GetMapping("/me")
    public ResponseEntity<UserDetails> whoAmI(){
        return ResponseEntity.ok(UserContext.getUser());
    }
}
